package org.itboys.mobile.controller.mobile;


import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.SundayAdvertisementDto;
import org.itboys.mobile.entity.mongo.SundayAdvertisement;
import org.itboys.mobile.service.mongo.front.SundayFrontAdvertisementService;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 作者：jiangxiong
 * 日期：2017年2月9日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_广告（新闻，轮播图）_controller。
 * 
 */
@Controller
@RequestMapping("/sunday/mobile/advertisement")
public class SundayMobileAdvertisementController extends BaseController {


	@Autowired
	private SundayFrontAdvertisementService advertisementService;

	/**---------------------------广告信息--------------------------**/
	/**
	 * 广告信息HTML5
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/html5",method={ RequestMethod.POST,RequestMethod.GET} )
	public  String html5(HttpServletRequest request,
						 Model model,
						 @RequestParam(value="id",required=true) long id){
		SundayAdvertisement advertisement = advertisementService.getById(id);
		model.addAttribute("advertisement",advertisement);
		return "/sunday/advertisement/html5";
	}

	/**
	 *
	 * @param response
	 * @param request
	 * @param zoneKey
	 */

	@RequestMapping(value="/getAdvertisementByZoneKey", method= { RequestMethod.POST,RequestMethod.GET} )
	@ApiOperation(value="【手机端】根据广告位获取广告信息(列表信息)。不分页")
	public void getAdvertisementByZoneKey(
			HttpServletResponse response,
		    HttpServletRequest request,
		    @ApiParam(required=true, name="zoneKey", value="广告位key。" )@RequestParam(value = "zoneKey",required = true) String zoneKey
		   ){
		try {
			List<SundayAdvertisementDto> advertisements = advertisementService.getAdvertisementByZone(zoneKey);
			ResponseMessage.success(advertisements);
		} catch (Exception e) {
			String msg ="";
			if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
				msg=e.getMessage();
			}else{
				msg= CommonConstants.ERROMSG;
				logger.info("/sunday/mobile/advertisement/getAdvertisementByZoneKey--------"+e);
			}
			e.printStackTrace();
			ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
		}
	}
	/**
	 *
	 * @param response
	 * @param request
	 * @param zoneKeys
	 */

	@RequestMapping(value="/getAdvertisementByZoneKeys", method= { RequestMethod.POST,RequestMethod.GET} )
	@ApiOperation(value="【手机端】批量多个广告位获取广告信息(列表信息)，可以传入多个广告位。不分页")
	public void getAdvertisementByZoneKeys(
			HttpServletResponse response,
		  	HttpServletRequest request,
		  	@ApiParam(required=true, name="zoneKeys")@RequestParam(value = "zoneKeys",required = true) List<String> zoneKeys
	) {
		try {
			List<Map<String,Object>> results = advertisementService.getAdvertisementByZoneKeys(zoneKeys);
			ResponseMessage.success(results);
		} catch (Exception e) {
			String msg ="";
			if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
				msg=e.getMessage();
			}else{
				msg= CommonConstants.ERROMSG;
				logger.info("/sunday/mobile/advertisement/getAdvertisementByZoneKey--------"+e);
			}
			e.printStackTrace();
			ResponseMessage.error(CommonConstants.FAIL_CODE,msg);


		}
	}
	/**
	 * 广告信息HTML5（直接根据广告位获取）
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/html5ByZoneCode",method={ RequestMethod.POST,RequestMethod.GET} )
	@ApiOperation(value="【手机端】根据广告位获取网页（例如：用户协议等），返回的是网页内容，直接加载")
	public  String html5ByZoneCode(
			HttpServletRequest request,
		 	Model model,
		 	@ApiParam(required=true, name="zoneKey")@RequestParam(value = "zoneKey",required = true) String zoneKey
	){
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("zoneKey",zoneKey);
		param.put("isDeleted",0);
		List<SundayAdvertisement> advertisements = advertisementService.list(param);
		if(ListUtil.isNotNull(advertisements)){
			model.addAttribute("advertisement",advertisements.get(0));
		}

		return "/sunday/advertisement/html5";
	}
}
